//index.js
//获取应用实例
const app = getApp()
import Toast from '../../miniprogram_npm/@vant/weapp/toast/toast.js';

Page({
  data: {
    value: '',
    list: [],
    activeNames: [],
    info: [
      { name: '1', title: "进行中", status: "PENDING" },
      { name: '2', title: "已完成", status: "FINISHED" },
      { name: '3', title: "已删除", status: "DELETED" },
    ],
    length: [0, 0, 0],
  },
  toSudoku(){
    wx.switchTab({
      url: '/pages/sudoku/sudoku'
    })
  },
  remove_fn(event) {
    let val = event.detail;
    switch (val.filter) {
      case 'PENDING':
        this.data.list[val.index].classify = 'DELETED'
        break;
      case 'FINISHED':
        this.data.list[val.index].classify = 'DELETED'
        break;
      case 'DELETED':
        this.data.list.splice(val.index, 1)
        break;
    }
    this.setData({
      list: this.data.list
    })
    this.computerLength()
    Toast.success('删除成功!');
  },
  toChangeStatus_child: function (event) {
    // console.log(event)
    let val = event.detail;
    switch (val.filter) {
      case 'PENDING':
        this.data.list[val.index].status = true
        this.data.list[val.index].classify = 'FINISHED'
        break;
      case 'FINISHED':
        this.data.list[val.index].status = false
        this.data.list[val.index].classify = 'PENDING'
        break;
    }
    this.setData({
      list: this.data.list
    })
    this.computerLength()
    Toast.success('修改状态成功!');
  },
  computerLength() {
    let length1 = this.data.list.filter(item => item.classify === 'PENDING').length;
    let length2 = this.data.list.filter(item => item.classify === 'FINISHED').length;
    let length3 = this.data.list.filter(item => item.classify === 'DELETED').length;
    this.data.length[0] = length1
    this.data.length[1] = length2
    this.data.length[2] = length3;
    this.setData({
      length: this.data.length
    })
  },
  confirm: function () {
    let index = this.data.list.findIndex((item) => this.data.value == item.text);
    if (index == -1) {
      let obj = {
        text: this.data.value,
        status: false,
        classify: 'PENDING'
      }
      this.data.list.push(obj);
      this.setData({
        list: this.data.list
      })
      this.computerLength()
      Toast.success('添加成功!');
    } else {
      Toast.fail('不能重复添加!');
    }
  },
  onChange(event) {
    console.log(event.detail);
    this.setData({
      value: event.detail
    })
  },
  onChangeIndex(event) {
    console.log(event)
    this.setData({
      activeNames: event.detail
    });
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    wx.setNavigationBarTitle({
      title: '首页',
    });
    if (app.globalData.userInfo) {
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  getUserInfo: function (e) {
    console.log(e)
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  }
})